#!/bin/sh
# toughradius stable_v2.0 install script
# author: jamiesun.net@gmail.com

rundir=/home/toughrun

usage ()
{
    cat <<EOF
    Usage: $0 [OPTIONS] instance

    docker_setup                install docker, docker-compose
    pull                        toughradius docker images pull
    install                     install toughradius with already exists mysql
    install_with_lmysql         install toughradius with local docker mysql instance
    install_with_rmysql         install toughradius with remote mysql server
    remove                      uninstall toughradius and database
    config                      toughradius instance config edit
    status                      toughradius instance status
    restart                     toughradius instance restart
    stop                        toughradius instance stop
    upgrade                     toughradius instance upgrade
    logs                        toughradius instance logs
    dsh                         toughradius instance bash term

    All other options are passed to the toughrad program.
EOF
        exit 1
}


install_with_lmysql()
{
    instance=${1:-toughradius}

    mkdir -p ${rundir}/$instance

    read -p "version [stable_v2,dev](stable_v2): " tr_release
    tr_release=${tr_release:-stable_v2}

    read -p "mysql user [raduser]: " mysql_user
    mysql_user=${mysql_user:-raduser}

    read -p "mysql user password [radpwd]: " mysql_password
    mysql_password=${mysql_password:-radpwd}

    read -p "mysql database [radiusd]: " mysql_database
    mysql_database=${mysql_database:-radiusd}

    read -p "mysql root password [radroot]: " mysql_root_password
    mysql_root_password=${mysql_root_password:-radroot}

    read -p "toughradius web port [1816]: " web_port
    web_port=${web_port:-1816}

    read -p "toughradius auth port [1812]: " auth_port
    auth_port=${auth_port:-1812}

    read -p "toughradius acct port [1813]: " acct_port
    acct_port=${acct_port:-1813}

    cat <<EOF

    ToughRADIUS instance config (create local mysql):

    instance name: $instance
    mysql_user: ${mysql_user}
    mysql_password: ${mysql_password}
    mysql_database: ${mysql_database}
    mysql_root_password: ${mysql_root_password}
    web_port: ${web_port}
    auth_port: ${auth_port}
    acct_port: ${acct_port}

EOF

    rm -f ${rundir}/$instance/docker-compose.yml
    cat > ${rundir}/$instance/docker-compose.yml <<EOF
raddb:
    container_name: raddb_${instance}
    image: "index.alauda.cn/toughstruct/mysql"
    privileged: true
    expose:
        - "3306"
    environment:
        - MYSQL_USER=$mysql_user
        - MYSQL_PASSWORD=$mysql_password
        - MYSQL_DATABASE=$mysql_database
        - MYSQL_ROOT_PASSWORD=$mysql_root_password
    restart: always
    volumes:
        - ${rundir}/$instance/trmysql:/var/lib/mysql
        - ${rundir}/$instance/backup:/var/backup

radius:
    container_name: radius_${instance}
    image: "index.alauda.cn/toughstruct/toughradius:${tr_release}"
    ports:
        - "${web_port}:1816"
        - "${auth_port}:1812/udp"
        - "${acct_port}:1813/udp"
    links:
        - raddb:raddb
    ulimits:
        nproc: 65535
        nofile:
          soft: 20000
          hard: 40000
    environment:
        - DB_TYPE=mysql
        - DB_URL=mysql://$mysql_user:$mysql_password@raddb:3306/$mysql_database?charset=utf8
    restart: always
    volumes:
        - ${rundir}/$instance:/var/toughradius
EOF

    cd ${rundir}/$instance

    cat ${rundir}/$instance/docker-compose.yml

    docker-compose up -d raddb

    echo 3
    sleep 1
    echo 2
    sleep 1
    echo 1
    sleep 1

    docker-compose up -d radius

    docker-compose ps

    exit 0
}

install()
{
    instance=${1:-toughradius}

    mkdir -p ${rundir}/$instance

    read -p "version [stable_v2,dev](stable_v2): " tr_release
    tr_release=${tr_release:-stable_v2}

    read -p "toughradius web port [1816]: " web_port
    web_port=${web_port:-1816}

    read -p "toughradius auth port [1812]: " auth_port
    auth_port=${auth_port:-1812}

    read -p "toughradius acct port [1813]: " acct_port
    acct_port=${acct_port:-1813}

    cat <<EOF

    ToughRADIUS instance config (with sqlite):

    instance name: $instance
    web_port: ${web_port}
    auth_port: ${auth_port}
    acct_port: ${acct_port}

EOF

    rm -f ${rundir}/$instance/docker-compose.yml
    cat > ${rundir}/$instance/docker-compose.yml  <<EOF
radius:
    container_name: radius_${instance}
    image: "index.alauda.cn/toughstruct/toughradius:${tr_release}"
    ports:
        - "${web_port}:1816"
        - "${auth_port}:1812/udp"
        - "${acct_port}:1813/udp"
    restart: always
    ulimits:
        nproc: 65535
        nofile:
          soft: 20000
          hard: 40000
    volumes:
        - ${rundir}/$instance:/var/toughradius
EOF

    cd ${rundir}/$instance

    cat ${rundir}/$instance/docker-compose.yml

    docker-compose up -d

    docker-compose ps

    exit 0
}

install_with_rmysql()
{
    instance=${1:-toughradius}

    mkdir -p ${rundir}/$instance

    read -p "version [stable_v2,dev](stable_v2): " tr_release
    tr_release=${tr_release:-stable_v2}

    read -p "mysql host (must): " mysql_host
    if [ -z $mysql_host ]; then
        echo "mysql host is empty"
        exit 1
    fi

    read -p "mysql port (3306): " mysql_port
    mysql_port=${mysql_port:-"3306"}

    read -p "mysql first init [0:false,1:true](0): " mysql_init
    mysql_init=${mysql_init:-"0"}

    read -p "mysql user [raduser]: " mysql_user
    mysql_user=${mysql_user:-raduser}

    read -p "mysql user password [radpwd]: " mysql_password
    mysql_password=${mysql_password:-radpwd}

    read -p "mysql database [radiusd]: " mysql_database
    mysql_database=${mysql_database:-radiusd}

    read -p "mysql root password [radroot]: " mysql_root_password
    mysql_root_password=${mysql_root_password:-radroot}

    read -p "toughradius web port [1816]: " web_port
    web_port=${web_port:-1816}

    read -p "toughradius auth port [1812]: " auth_port
    auth_port=${auth_port:-1812}

    read -p "toughradius acct port [1813]: " acct_port
    acct_port=${acct_port:-1813}

    cat <<EOF

    ToughRADIUS instance config (with remote mysql):

    instance name: $instance
    mysql_host: ${mysql_host}
    mysql_port: ${mysql_port}
    mysql_user: ${mysql_user}
    mysql_password: ${mysql_password}
    mysql_database: ${mysql_database}
    mysql_root_password: ${mysql_root_password}
    web_port: ${web_port}
    auth_port: ${auth_port}
    acct_port: ${acct_port}

EOF

    rm -f ${rundir}/$instance/docker-compose.yml
    cat > ${rundir}/$instance/docker-compose.yml  <<EOF
radius:
    container_name: radius_${instance}
    image: "index.alauda.cn/toughstruct/toughradius:${tr_release}"
    ports:
        - "${web_port}:1816"
        - "${auth_port}:1812/udp"
        - "${acct_port}:1813/udp"
    environment:
        - DB_INIT=$mysql_init
        - DB_TYPE=mysql
        - DB_URL=mysql://$mysql_user:$mysql_password@$mysql_host:$mysql_port/$mysql_database?charset=utf8
    restart: always
    ulimits:
        nproc: 65535
        nofile:
          soft: 20000
          hard: 40000    
    volumes:
        - ${rundir}/$instance:/var/toughradius
EOF

    cd ${rundir}/$instance

    cat ${rundir}/$instance/docker-compose.yml

    docker-compose up -d

    docker-compose ps

    exit 0
}


docker_setup()
{
    curl -sSL https://get.daocloud.io/docker | sh

    curl -L https://get.daocloud.io/docker/compose/releases/download/1.5.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
    chmod +x /usr/local/bin/docker-compose

    ln -s /usr/local/bin/docker-compose /usr/local/bin/docp

    service docker start
}


remove()
{
    instance=${1:-toughradius}

    cd ${rundir}/$instance
    read -p "Will delete all, do you confirm? [y/n](n): " is_rm
    is_rm=${is_rm:-n}
    if [ $is_rm == "y" ]; then
        docker-compose kill
        docker-compose rm
        rm -fr ${rundir}/$instance/trmysql
        rm -f ${rundir}/$instance/.install
    fi 
}


config()
{
    instance=${1:-toughradius}    
    vi ${rundir}/$instance/docker-compose.yml
}

status()
{
    instance=${1:-toughradius}    
    cd ${rundir}/$instance
    docker-compose ps
}

restart()
{
    instance=${1:-toughradius}      
    cd ${rundir}/$instance
    docker-compose restart $2
    docker-compose ps
}

stop()
{
    instance=${1:-toughradius}    
    cd ${rundir}/$instance
    docker-compose stop $2
    docker-compose ps
}

upgrade()
{
    instance=${1:-toughradius}    
    cd ${rundir}/$instance
    docker exec -it radius_$instance /usr/local/bin/toughrun upgrade
    docker-compose restart
    docker-compose ps
}

uplib()
{
    instance=${1:-toughradius}    
    cd ${rundir}/$instance
    docker exec -it radius_$instance /usr/local/bin/toughrun uplib
    docker-compose restart 
    docker-compose ps
}

logs()
{
    instance=${1:-toughradius}
    cd ${rundir}/$instance
    tail -f ./toughradius.log
}

dsh()
{
    instance=${1:-toughradius}    
    cd ${rundir}/$instance
    docker exec -it radius_$instance bash
}


pull()
{
    docker pull index.alauda.cn/toughstruct/mysql
    docker pull index.alauda.cn/toughstruct/toughradius:stable_v2
    docker pull index.alauda.cn/toughstruct/toughradius:dev
}


case "$1" in

  docker_setup)
    docker_setup
  ;;

  install)
    install $2
  ;;

  install_with_lmysql)
    install_with_lmysql $2
  ;;

  install_with_rmysql)
    install_with_rmysql $2
  ;;

  remove)
    remove $2
  ;;

  config)
    config $2
  ;;

  status)
    status $2
  ;;

  restart)
    restart $2 $3
  ;;

  stop)
    stop $2 $3
  ;;

  logs)
    logs $2
  ;;

  dsh)
    dsh $2
  ;;

  pull)
    pull
  ;;

  upgrade)
    upgrade $2
  ;;

  uplib)
    uplib $2
  ;;


  *)
   usage
  ;;

esac